/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/string.h"

/**
 * @brief Find the first occurrence of a character in a memory block.
 *
 * @param str The memory block to search.
 * @param c The character to find.
 * @param n The number of bytes to search.
 * @return A pointer to the first occurrence of c, or NULL if not found.
 */
void *Smemchr(const void *str, int c, size_t n) {
    if (str == NULL || n == 0)
        return NULL;

    const unsigned char *s = (const unsigned char *)str;

    while (n--) {
        if (*s == (unsigned char)c) {
            return (void *)s;
        }
        s++;
    }
    return NULL;
}
